
--[[
-- Author:      feilong
-- DateTime:    2019-01-029 00:05:48
-- Description: 日志打印
-- 
-- 

--]]

local skynet = require("skynet")

local table_insert = table.insert 
local table_concat = table.concat
local string_format = string.format

--c语言printf 输出带颜色的文本
local ANSI_COLOR_RED = "\x1b[31m"
local ANSI_COLOR_GREEN = "\x1b[32m"
local ANSI_COLOR_YELLOW = "\x1b[33m"
local ANSI_COLOR_BLUE = "\x1b[34m"
local ANSI_COLOR_MAGENTA = "\x1b[35m"
local ANSI_COLOR_CYAN = "\x1b[36m"
local ANSI_COLOR_RESET = "\x1b[0m"



local Logger = class("Logger")

function Logger:ctor()
	
end


--函数调用信息
function Logger:funcInfo(...)
	local info = debug.getinfo(3, "nlS")		
	local time = os.date("%Y-%m-%d %H:%M:%S",os.time())
	local func_name = info.name
    if func_name and func_name ~= '?' then
    	func_name = ' ::' .. func_name
    else
    	func_name = '' 
    end
	local service_name = g.service_name
	if not service_name then 
		service_name = ""
	end    
	local content = string_format('[%s][%s] <%s:%d%s> ',service_name, time, info.source, info.currentline, func_name)
	return content
end

--设置内容的颜色
function Logger:setColor(content, color)
	return color ..content .. ANSI_COLOR_RESET
end

function Logger:debug(...)
	-- tostring 在luaex里做了处理
	local args = {...}
	local content = ""
	for k, v in pairs(args) do 
		content = content .." ".. tostring(v)
	end
	local str = self:setColor("[debug]" .. self:funcInfo() .. content, ANSI_COLOR_GREEN)
	skynet.error(str)
end

function Logger:info(...)
	local args = {...}
	local content = ""
	for k, v in pairs(args) do 
		content = content .. tostring(v)
	end	
	local str = self:setColor("[info]" .. self:funcInfo() .. content, ANSI_COLOR_GREEN)
	skynet.error(str)
end

function Logger:waring(...)
	local args = {...}
	local content = ""
	for k, v in pairs(args) do 
		content = content .. tostring(v)
	end
		
	local str = self:setColor("[waring]" .. self:funcInfo() .. content, ANSI_COLOR_YELLOW)
	skynet.error(str)
end

function Logger:error(...)
	local args = {...}
	local content = ""
	for k, v in pairs(args) do 
		content = content .." ".. tostring(v)
	end
		
	local str = self:setColor("[error]" .. self:funcInfo() .. content, ANSI_COLOR_RED)
	skynet.error(str)
end

function Logger:fatal(...)
	local args = {...}
	local content = ""
	for k, v in pairs(args) do 
		content = content .. tostring(v)
	end
		
	local str = self:setColor("[fatal]" .. self:funcInfo() .. content, ANSI_COLOR_MAGENTA)
	skynet.error(str)
end


return Logger